package com.topisv.tms.service.yto;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.topisv.tms.entity.Consignor;
import com.topisv.tms.entity.Customers;
import com.topisv.tms.entity.EdiOrder;
import com.topisv.tms.entity.EdiOrderItem;
import com.topisv.tms.entity.work.WorkOrder;
import com.topisv.tms.jms.vo.ConsignorMessage;
import com.topisv.tms.jms.vo.Ordeno;
import com.topisv.tms.jms.vo.OrdenoGoods;
import com.topisv.tms.jms.vo.SendMessage;
import com.topisv.tms.service.ConsignorService;
import com.topisv.tms.service.CustomersService;
import com.topisv.tms.service.EdiOrderItemService;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.service.work.WorkOrderService;

@Component
public class YTOCreateOrdenoService {

	@Autowired
	private ConsignorService consignorService;
	@Autowired
	private CustomersService customersService;
	@Autowired
	private EdiOrderService EdiOrderService;
	@Autowired
	private EdiOrderItemService ediOrderItemService;
	@Autowired
	private WorkOrderService workOrderService;
	
	
	public SendMessage createYTOOrdeno(String ordeno)throws Exception{
		try {
			WorkOrder workOrder=workOrderService.loadOrderByOrderno(ordeno);
			Customers customers=customersService.findByCustomerCode(workOrder.getCustomerCode());
			Consignor c=consignorService.findCustomersId(customers.getId());
			ConsignorMessage consignorMessage=new ConsignorMessage();
			if(c==null){
				c=new Consignor();
				consignorMessage.setAddress("广东省深圳市罗湖科技大厦501");
				consignorMessage.setCity("深圳");
				consignorMessage.setCustomersId(c.getCustomersId());
				consignorMessage.setDepositoryName(c.getDepositoryName());
				consignorMessage.setDistrict(c.getDistrict());
				consignorMessage.setIsDefault(c.getDefaultStatus());
				consignorMessage.setMobile("13421338270");
				consignorMessage.setName("派友电子商务有限公司");
				consignorMessage.setPhone("075506204082");
				consignorMessage.setProvince(c.getProvince());
				consignorMessage.setSalesRerurnAddress(c.getSalesRerurnAddress());
				consignorMessage.setSalesRerurnMessage(c.getSalesRerurnMessage());
				consignorMessage.setStatus(c.getStatus());
				consignorMessage.setUgc(c.getUgc());
				consignorMessage.setZipcode(c.getZipcode());
			}else{
				consignorMessage.setAddress(c.getAddress());
				consignorMessage.setCity(c.getCity());
				consignorMessage.setCustomersId(c.getCustomersId());
				consignorMessage.setDepositoryName(c.getDepositoryName());
				consignorMessage.setDistrict(c.getDistrict());
				consignorMessage.setIsDefault(c.getDefaultStatus());
				consignorMessage.setMobile(c.getMobile());
				consignorMessage.setName(c.getName());
				consignorMessage.setPhone(c.getPhone());
				consignorMessage.setProvince(c.getProvince());
				consignorMessage.setSalesRerurnAddress(c.getSalesRerurnAddress());
				consignorMessage.setSalesRerurnMessage(c.getSalesRerurnMessage());
				consignorMessage.setStatus(c.getStatus());
				consignorMessage.setUgc(c.getUgc());
				consignorMessage.setZipcode(c.getZipcode());
			}
			EdiOrder ediOrder=EdiOrderService.findByOrdeno(ordeno);
			List<EdiOrderItem> ediOrderItems=ediOrderItemService.findByOrdeno(ordeno);
			
			Ordeno o=new Ordeno();
			o.setExpressNo(workOrder.getEbillNo());
			o.setOrdeno(ordeno);
			o.setReceiverAddress(ediOrder.getAddres());
			o.setReceiverCity(ediOrder.getCityna());
			o.setReceiverMobile("18000000000");
			o.setReceiverName(ediOrder.getRecena());
			o.setReceiverPhone("18000000000");
			o.setReceiverState(ediOrder.getProvna());
			o.setReceiverZip(ediOrder.getPostco());
			o.setTotalPrice(ediOrder.getActuallyPaid());
			o.setWhcode(workOrder.getWarehouseCode());
			List<OrdenoGoods> ordenoGoods=new ArrayList<OrdenoGoods>();
			for(EdiOrderItem eoi:ediOrderItems){
				OrdenoGoods og=new OrdenoGoods();
				og.setPrice(eoi.getPri());
				og.setQuantity(eoi.getQty());
				og.setSku(eoi.getSku());
				ordenoGoods.add(og);
			}
			o.setOrdenoGoods(ordenoGoods);
			
			
			
			ObjectMapper objectMapper = new ObjectMapper();
			Map map = new HashMap();
			map.put("consignor", consignorMessage);
			map.put("ordeno", o);
			String message=objectMapper.writeValueAsString(map);
			SendMessage sendMessage=new SendMessage();
			sendMessage.setType("createOrdenoYTO");
			sendMessage.setMessage(message);
			return sendMessage;
		} catch (Exception e) {
			throw e;
		}
		
	}
}
